package com.luyuan.crm.report.delivery.web;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.luyuan.crm.report.delivery.DeliveryReport;
import com.luyuan.crm.report.delivery.DeliveryReportService;
import com.luyuan.crm.util.ExcelUtil;
import com.ruijie.web.jsf.action.BaseAction;
import com.ruijie.web.jsf.context.TableContext;
import com.ruijie.foundation.exception.BusinessException;

@Component("deliveryReportAction")
@Scope("view")
public class DeliveryReportAction extends BaseAction {
    private static final Log logger = LogFactory.getLog(DeliveryReportAction.class);

    @Resource(name = "deliveryReportService")
    private DeliveryReportService deliveryReportService;

    private Date date = new Date();

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public DataModel getDataModel() {
        List<DeliveryReport> reports = deliveryReportService.findByDate(date);

        TableContext.getTablePage().setTotalRow(reports.size());
        return new ListDataModel(reports);
    }

    public void generationReport() {
        deliveryReportService.generationReport();
    }

    public void exportReport() {
        List<DeliveryReport> reports = deliveryReportService.findByDate(date);
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = ExcelUtil.createSheet(wb, format.format(date));

        HSSFRow headRow = sheet.createRow(0);
        Map<Integer, String> headerMap = new HashMap<Integer, String>();
        headerMap.put(0, "产品名称");
        headerMap.put(1, "产品规格");
        headerMap.put(2, "出货数量");
        ExcelUtil.createHeader(headRow, headerMap);

        int len = reports.size();
        for (int i = 0; i < len; i++) {
            DeliveryReport report = reports.get(i);

            HSSFRow row = ExcelUtil.createRow(sheet, i + 1);
            ExcelUtil.createCell(row, (short) 0).setCellValue(new HSSFRichTextString(report.getProduct().getName()));
            ExcelUtil.createCell(row, (short) 1).setCellValue(
                    new HSSFRichTextString(report.getProduct().getSpecification()));
            ExcelUtil.createCell(row, (short) 2).setCellValue(report.getDeliveryCount());
        }

        synchronized (ExcelUtil.class) {
            try {
                ExcelUtil.exportExcel(wb, "发货记录.xls");
            } catch (IOException e) {
                logger.error("export shippedReport error!", e);
                addGlobalI18nMessage(new BusinessException("导出数据失败！", e));
            }
        }

    }
}
